package com.n5x.controller.front.promotion;

import com.n5x.common.base.JsonModel;
import com.n5x.common.base.ResponseCode;
import com.n5x.controller.BaseController;
import com.n5x.mapper.promotion.PromotionMapper;
import com.n5x.model.promotion.Promotion;
import com.n5x.service.IPromotionService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品接口Controller
 * 需要后台管理员token
 */
@Controller("promotion")
@RequestMapping("promotion")
public class PromotionController extends BaseController {

    @Autowired
    IPromotionService iPromotionService;

    @RequestMapping(value = "/findPromotions")
    @ResponseBody
    public JsonModel findPromotions(@RequestParam(value = "token", required = false) String token, HttpServletRequest request) {
        try {
            String disable = request.getParameter("disable");
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(disable)) {
                Boolean d = disable.equals("1") ? true : (disable.equals("0") ? false : null);
                params.put(PromotionMapper.FIELD_DISABLE, d);
            }
            List<Promotion> list = iPromotionService.findAll(params);
            if (CollectionUtils.isEmpty(list)) {
                return getResultError(ResponseCode.Common_DataNotFound, "商品活动列表为空");
            }
            return getResult(list);
        } catch (Exception e) {
            logger.error("[查询商品活动列表失败]e:{}", new Object[]{e.toString()});
            e.printStackTrace();
            return getResultError(ResponseCode.Common_Error, e.toString());
        }
    }
}